﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using RCA1._0;
using System.Data.OleDb;

namespace Royal_1._0_DAL_WS
{
    public class LoaiNganhNgheDAO: DataProvider
    {
        public DataTable LayBang()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAINGANHNGHE";
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        #region "Lấy danh sách"

        public List<LoaiNganhNgheDTO> LayDanhSach()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAINGANHNGHE";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            LoaiNganhNgheDTO tempDTO;
            List<LoaiNganhNgheDTO> dsLOAINGANHNGHE = new List<LoaiNganhNgheDTO>();
            while (dr.Read())
            {
                tempDTO = new LoaiNganhNgheDTO();
                tempDTO.IDLoaiNganhNghe = Convert.ToInt32(dr["IDLoaiNganhNghe"]);
                tempDTO.TenLoaiNganhNghe = dr["TenLoaiNganhNghe"].ToString();
                dsLOAINGANHNGHE.Add(tempDTO);
            }
            return dsLOAINGANHNGHE;
        }

        #endregion


        #region "Lấy danh sách mã loại"

        public List<LoaiNganhNgheDTO> LayMaLoaiNganhNgheKinhDoanh()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAINGANHNGHE";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            LoaiNganhNgheDTO tempDTO;
            List<LoaiNganhNgheDTO> dsLOAINGANHNGHE = new List<LoaiNganhNgheDTO>();
            while (dr.Read())
            {
                tempDTO = new LoaiNganhNgheDTO();
                tempDTO.IDLoaiNganhNghe = Convert.ToInt32(dr["IDLoaiNganhNghe"]);
                dsLOAINGANHNGHE.Add(tempDTO);
            }
            return dsLOAINGANHNGHE;
        }

        #endregion


        #region "Tìm kiếm"
        public LoaiNganhNgheDTO TimTheoMa(int MaLoaiNganhNghe)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAINGANHNGHE Where IDLoaiNganhNghe=" + MaLoaiNganhNghe.ToString();
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            dr.Read();
            LoaiNganhNgheDTO tempDTO = new LoaiNganhNgheDTO();
            tempDTO.IDLoaiNganhNghe = Convert.ToInt32(dr["IDLoaiNganhNghe"]);
            tempDTO.TenLoaiNganhNghe = dr["TenLoaiNganhNghe"].ToString();
            cn.Close(); return tempDTO;
        }

        #endregion


        #region "Thêm"

        public void Them(LoaiNganhNgheDTO tempDTO)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "insert into LOAINGANHNGHE(TenLoaiNganhNghe) values (@TenLoaiNganhNghe)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiNganhNghe", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiNganhNghe;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion


        #region "Xóa"

        public void Xoa(int MaLoaiNganhNghe)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "delete from LOAINGANHNGHE Where IDLoaiNganhNghe = " + MaLoaiNganhNghe.ToString();
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion


        #region "Sửa"

        public void Sua(LoaiNganhNgheDTO tempDTO)
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "update LOAINGANHNGHE set TenLoaiNganhNghe = @TenLoaiNganhNghe where IDLoaiNganhNghe = @IDLoaiNganhNghe";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiNganhNghe", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiNganhNghe;
            cmd.Parameters.Add("@IDLoaiNganhNghe", OleDbType.Integer).Value = tempDTO.IDLoaiNganhNghe;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        #endregion

    }
}